package controllers.manage.fitness;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.fitness.Fitnesses;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.Map;

/**
 * 健身类型管理
 * Created by hanbin on 17/4/6.
 */
@With(BaseController.class)
public class FitnessCtrl extends Controller{

    /**
     * 跳转到健身类型管理页面
     */
    public static void index(Integer pageNumber, String name){

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtil.isNotBlank(name)) {
            searchMap.put("name", "%"+name+"%");
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        flash.put("menuId", "8");

        JPAExtPaginator<Fitnesses> resultPage = Fitnesses.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, name);
    }

    /**
     * 跳转到新增健身类型页面
     */
    public static void add(){
        render();
    }

    /**
     * 跳转到修改健身类型页面
     */
    public static void modify(long id, Integer pageNumber){
        Fitnesses fitness = Fitnesses.findById(id);
        render(fitness, pageNumber);
    }


    /**
     * 创建健身类型
     */
    public static void create(Fitnesses fitness){

        Merchant merchant = Secure.getLoginUser().merchant;
        fitness.merchant = merchant;

        fitness.deleted = DeletedStatus.UN_DELETED;
        fitness.save();
        flash.put("msg" , "健身类型创建成功");
        redirect("/BS0013/admin/fitness/index/1");
    }

    /**
     * 修改健身类型
     */
    public static void update(Long id, Fitnesses fitness, Integer pageNumber){
        Fitnesses.update(id , fitness);
        flash.put("msg" , "健身类型修改成功");
        redirect("/BS0013/admin/fitness/index/"+pageNumber);
    }

    /**
     * 删除健身类型
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Fitnesses fitness = Fitnesses.findById(id);
        fitness.deleted=DeletedStatus.DELETED;
        fitness.save();
        flash.put("msg" , "健身类型删除成功");
        redirect("/BS0013/admin/fitness/index/"+pageNumber);
    }
}
